/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.istia.gesstages.actions;

import fr.istia.gesstages.metier.*;
import fr.istia.gesstages.util.DateFormat;
import java.util.*;
import java.util.HashMap;

/**
 *
 * @author Gaëtan
 */
@SuppressWarnings("serial")
public class ApplyAction extends ParentAction {
    
    private List<Apply> applies = new ArrayList<Apply>();
    private List<User> users = new ArrayList<User>();
    private List<Offer> offers = new ArrayList<Offer>();
    private Map<User, List<Apply>> appliesByStudent = new HashMap<User, List<Apply>>();
    private Map<Offer, List<Apply>> appliesByOffer = new HashMap<Offer, List<Apply>>();
    private int offerid;
    private int id;
    private Offer offer;

    public String findApplyByStudent() {
        try {
            users = metier.findUsersByType(metier.findUserTypeByName("Etudiant"));
            for (User user : users) {
                applies = metier.findAppliesByUser(user);
                appliesByStudent.put(user, applies);
            }
        } catch (Exception ex) {
            addActionError(getText("error.apply.order.student") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String findApplyByOffer() {
        try {
            offers = metier.findAllOffers();
            for (Offer offer : offers) {
                applies = metier.findAppliesByOffer(offer);
                appliesByOffer.put(offer, applies);
            }
        } catch (Exception ex) {
            addActionError(getText("error.apply.order.offer") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String applyAttribute() {
        try {
            users = metier.findUsersByType(metier.findUserTypeByName("Etudiant"));
            offers = metier.findOffersNotAttributed();
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.user") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String validateAttributeApply() {
        try {
            User user = metier.findUserById(id);
            user.setAttribution(metier.findOfferById(offerid));
            metier.updateUser(user);
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.validate") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }
    
    public String removeAttributeApply(){
        try {
            User user = metier.findUserById(id);
            user.setAttribution(null);
            metier.updateUser(user);
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.validate") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }
    
    public String applyOffer(){
        try {
            Apply a = new Apply();
            a.setStage(metier.findOfferById(id));
            a.setUser((User) session.get("user"));
            a.setDate(DateFormat.getGregorianDate(new Date()));
            metier.createApply(a);
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.validate") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }

    public String applyUser(){
        try {
            session.put("user",metier.findUserByLogin(((User)session.get("user")).getLogin()));
            applies = metier.findAppliesByUser((User) session.get("user"));
            if(((User) session.get("user")).getAttribution()!=null)
                offer = ((User) session.get("user")).getAttribution();
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.validate") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }
    
    public String removeApply(){
        try {
            metier.deleteApply(metier.findApplyById(id));
        } catch (Exception ex) {
            addActionError(getText("error.apply.attribute.validate") + " : " + ex.toString());
            return ERROR;
        }
        return SUCCESS;
    }
    
    public List<Apply> getApplies() {
        return applies;
    }

    public void setApplies(List<Apply> applies) {
        this.applies = applies;
    }

    public List<Offer> getOffers() {
        return offers;
    }

    public void setOffers(List<Offer> offers) {
        this.offers = offers;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public Map<Offer, List<Apply>> getAppliesByOffer() {
        return appliesByOffer;
    }

    public void setAppliesByOffer(Map<Offer, List<Apply>> appliesByOffer) {
        this.appliesByOffer = appliesByOffer;
    }

    public Map<User, List<Apply>> getAppliesByStudent() {
        return appliesByStudent;
    }

    public void setAppliesByStudent(Map<User, List<Apply>> appliesByStudent) {
        this.appliesByStudent = appliesByStudent;
    }

    public int getOfferid() {
        return offerid;
    }

    public void setOfferid(int offerid) {
        this.offerid = offerid;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Offer getOffer() {
        return offer;
    }

    public void setOffer(Offer offer) {
        this.offer = offer;
    }
}
